package com.sinosoft.perfect.system._display.dataFormat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sinosoft.perfect.common.xmlService.XmlInfoBean;
import com.sinosoft.perfect.common.xmlService.XmlProducerDom4j;
import com.sinosoft.perfect.system._.po.TResourceSystem;

public class Model2TreeDataFormat implements IModel2TreeDataFormat {

	@Override
	public String dataFormatModel2Tree(List<TResourceSystem> resourceList) {
		List<XmlInfoBean> infos = null;
		if (resourceList != null && resourceList.size() > 0) {
			if (infos == null) {
				infos = new ArrayList<XmlInfoBean>();
				XmlInfoBean info = new XmlInfoBean();
				info.setLevel("0");
				info.setMyid("0");
				info.setName("tree");
				Map<String, String> properties = new HashMap<String, String>();
				properties.put("id", "0");
				info.setProperties(properties);
				infos.add(info);
			}
			for (int i = 0; i < resourceList.size(); i++) {
				TResourceSystem resource = resourceList.get(i);
				XmlInfoBean info = new XmlInfoBean();

				info.setMyid(resource.getResourceId());
				info.setParentId(resource.getSuperResourceId());
				info.setName("item");
				Map<String, String> properties = new HashMap<String, String>();
				properties.put("id", resource.getResourceId());
				properties.put("text", resource.getResourceName());
				properties.put("im0", resource.getResourceImgName()==null?"defaultResource.gif":resource.getResourceImgName());
				properties.put("im1", resource.getResourceImgName()==null?"defaultResource.gif":resource.getResourceImgName());
				properties.put("im2", resource.getResourceImgName()==null?"defaultResource.gif":resource.getResourceImgName());
				info.setProperties(properties);
				infos.add(info);
			}
		}
		return XmlProducerDom4j.generateXMLString(infos);
	}

}
